unit frmConfigSound;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, fmoddyn, fmodtypes;


type
  TOutput = record
    Value: TFSoundOutputTypes;
    Name: String;
  end;

const
{$IFDEF MSWINDOWS}
  Output: array [0..4] of TOutput = (
    ( Value: FSOUND_OUTPUT_DSOUND;  Name: 'DirectSound' ),
    ( Value: FSOUND_OUTPUT_WINMM;   Name: 'Windows Multimedia' ),
    ( Value: FSOUND_OUTPUT_A3D;     Name: 'A3D' ),
    ( Value: FSOUND_OUTPUT_ASIO;    Name: 'ASIO' ),
    ( Value: FSOUND_OUTPUT_NOSOUND; Name: 'No sound' )
  );

{$ELSE}
{$IFDEF LINUX}
  Output: array [0..3] of TOutput = (
    ( Value: FSOUND_OUTPUT_OSS;     Name: 'Open Sound System' ),
    ( Value: FSOUND_OUTPUT_ESD;     Name: 'Enlightenment Sound Daemon' ),
    ( Value: FSOUND_OUTPUT_ALSA;    Name: 'ALSA' ),
    ( Value: FSOUND_OUTPUT_NOSOUND; Name: 'No sound' )
  );
{$ENDIF}
{$ENDIF}

type
  TformConfigSound = class(TForm)
    lbxOutput: TListBox;
    lbxDriver: TListBox;
    Label1: TLabel;
    Label2: TLabel;
    cmdValider: TButton;
    cmdAnnuler: TButton;
    procedure cmdValiderClick(Sender: TObject);
    procedure lbxOutputClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    ChoixOutput: integer;
    ChoixDriver: integer;

    DefautOutput: integer;
    DefautDriver: integer;

    procedure ReloadOutput;

  end;

var
  formConfigSound: TformConfigSound;

implementation

{$R *.dfm}

procedure TformConfigSound.cmdValiderClick(Sender: TObject);
begin
   { If no output type is selected, then exit }
   if lbxOutput.ItemIndex >= 0 then begin
      ChoixOutput := Integer(lbxOutput.Items.Objects[lbxOutput.ItemIndex]);

      if lbxDriver.ItemIndex >= 0 then begin
         ChoixDriver := lbxDriver.ItemIndex;
      end else begin
         ChoixOutput := -1;
      end;

   end;


end;

procedure TformConfigSound.ReloadOutput;
var Index: integer;
    bFound: boolean;
begin

  bFound := false;
  lbxOutput.Items.Clear;
  

  { Populate the list of output types with both the name and enumeration value }
  { Use a typecasting trick to store an integer value as an object }
  for Index := Low(Output) to High(Output) do begin

    lbxOutput.Items.AddObject(Output[Index].Name, TObject(Output[Index].Value));

    if Ord(Output[Index].Value) = DefautOutput then begin
       lbxOutput.ItemIndex := lbxOutput.Count - 1;
       bFound := true;
    end;
  end;

  if bFound then begin
     // charge les drivers
     lbxOutputClick(nil);

     if DefautDriver < lbxDriver.Count then begin
        lbxDriver.ItemIndex := DefautDriver;
     end;

  end;

end;

procedure TformConfigSound.FormCreate(Sender: TObject);
var Index: integer;
begin
  { Populate the list of output types with both the name and enumeration value }
  { Use a typecasting trick to store an integer value as an object }
  for Index := Low(Output) to High(Output) do
    lbxOutput.Items.AddObject(Output[Index].Name, TObject(Output[Index].Value));


  ChoixOutput := -1;
  ChoixDriver := -1;
end;

procedure TformConfigSound.lbxOutputClick(Sender: TObject);
var Index: Integer;
begin
  { Clear the existing list }
  lbxDriver.Items.Clear;

  { If no output type is selected, then exit }
  if lbxOutput.ItemIndex < 0 then
    Exit;

  { Set the output type }
  FSOUND_SetOutput(TFSoundOutputTypes(lbxOutput.Items.Objects[lbxOutput.ItemIndex]));

  { Get the list of available drivers }
  for Index := 0 to FSOUND_GetNumDrivers - 1 do
    lbxDriver.Items.Add(FSOUND_GetDriverName(Index));

  { Select the first driver in the list }
  if lbxDriver.Items.Count > 0 then
    lbxDriver.ItemIndex := 0;

end;

end.
